﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;
using NangLuongTieuDiemDTO.DTO;
using NangLuongTieuDiemBUS;

namespace NangLuongTieuDiemGUI.NangLuongTieuDiemForms
{
    public partial class frmCauHinhSql : DevExpress.XtraEditors.XtraForm
    {
        //chuỗi connection đọc được
        private string strConnection = "";

        #region "[khởi tạo cơ bản]"
        public frmCauHinhSql()
        {
            InitializeComponent();
        }
        #endregion

        #region "[Load]"
        private void frmCauHinhSql_Load(object sender, EventArgs e)
        {
            try
            {
                rdwindows.Checked = true;
                SetStateControlWhileChosenRadibutton(rdwindows.Checked);
              
            }
            catch(Exception ex) 
            {
                InSert_LoiHeThong("frmCauHinhSql_Load", ex);
            }
        }
        #endregion

        #region "[trang thái control]"
        private void SetStateControlWhileChosenRadibutton(bool _isFlag)
        {
            try
            {
                txtUserName.Enabled = txtPassword.Enabled = !_isFlag;
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("SetStateControlWhileChosenRadibutton", ex);
            }
        }
        #endregion

        #region "[sự kiện khi bấm vào nút radiobutton]"
        private void rdwindows_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                SetStateControlWhileChosenRadibutton(true);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("rdwindows_CheckedChanged", ex);
            }
        }
        
        private void rdsqlserver_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                SetStateControlWhileChosenRadibutton(false);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("rdsqlserver_CheckedChanged", ex);
            }
        }
        #endregion

        #region "[nút kiểm tra]"
        private void btnTest_Click(object sender, EventArgs e)
        {

            if (txtSeverName.Text.Trim().Length == 0)
            {
                MessageBox.Show("Server Name Don't Null", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (strConnection != null)
                MessageBox.Show("Test conection Successfull", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show("Test conection Fail", "Eror", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        #endregion

        #region "[nút ok]"
        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                //lấy thông tin tên database.
                string nameDatabase = cmbDatabaseName.SelectedItem.ToString();
                strConnection = strConnection + "database=" + nameDatabase;
                //thực hiện cấu hình đường dẫn vào file text.


                //ghi file text = 
                ClassUtils.Utils.WriteFileText(strConnection);

                this.Close();

            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("btnOK_Click", ex);
            }
        }
        #endregion

        #region "[hủy thao tác]"
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region "[Refresh]"
        private void btnReresh_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtSeverName.Text.Trim().Length == 0)
                {
                    MessageBox.Show("Server Name Don't Null", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                //lấy tên server name và user name , mật khẩu.
                string strServerName = txtSeverName.Text.Trim();
                string strUserName = txtUserName.Text.Trim();
                string strPassword = txtPassword.Text.Trim();
                List<string> listDatabaseName = new List<string>();


                if (rdsqlserver.Checked)
                    strConnection = "server=" + strServerName + ";uid=" + strUserName + ";pwd=" + strPassword;

                if (rdwindows.Checked)
                    strConnection = @"Server=" + strServerName + ";Integrated Security=SSPI;";

                SqlConnection SqlCon = new SqlConnection(strConnection);
                try
                {
                    SqlCon.Open();
                }
                catch (Exception ex)
                {
                    InSert_LoiHeThong("sqlCon", ex);
                    ClassUtils.Utils.MassageBoxErrorOtInformation("Không tìm thấy servername này", "Thông báo", true);
                    return;
                }


                SqlCommand SqlCom = new SqlCommand("SELECT name FROM sys.databases", SqlCon);

                SqlDataReader SqlDR;
                SqlDR = SqlCom.ExecuteReader();

                while (SqlDR.Read())
                {
                    listDatabaseName.Add(SqlDR["name"].ToString());
                }

                //load danh sách các database name lên combobox
                cmbDatabaseName.DataSource = listDatabaseName;

            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("btnReresh_Click", ex);
            }
        }
        #endregion


        #region "[Hàm insert lỗi hệ thống]"
        public void InSert_LoiHeThong(string TenHam, Exception ex)
        {
            LoiHeThongDTO lhtDto = new LoiHeThongDTO();
            lhtDto.TenLoi = ex.Message.ToString();
            lhtDto.NgayTao = DateTime.Now;
            lhtDto.TenHamBiLoi = TenHam;
            lhtDto.SuaLoi = false;
            lhtDto.GhiChu = "";
            NangLuongTieuDiem_Management.LoiHeThong_Insert(lhtDto);
            MessageBox.Show(lhtDto.TenLoi);
        }
        #endregion


    }
}